﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using TA.DotNetUtils;
using TA.JSON;
using TA.Turbo;
using TA.Turbo.Controls;

public partial class Default : TPage
{
	const int NumRecords = 1000;
	static readonly Random _rnd = new Random();
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		this.Includes.Add(Script.ElementSearch);
	}
	protected override void OnLoad(EventArgs e)
	{
		base.OnLoad(e);
		if (!Page.IsPostBack)
		{
			chk0.Value = this.rpt.Groupings[0].Enabled = false;
			chk1.Value = this.rpt.Groupings[1].Enabled = false;

			Bind();
		}
	}

	private void Bind()
	{
		List<T> r = new List<T>();
		for (int i = 0; i < int.Parse(this.txtNumRecords.Value); ++i)
		{
			r.Add(new T
			{
				A = ((char)(_rnd.Next(0, 5) + 'A')).ToString(),
				B = ((char)(_rnd.Next(0, 5) + 'A')).ToString(),
				C = _rnd.Next(0, 5),
			});
		}
		rpt.Value.Entries.AddRange(r);
	}
	protected void chk_ValueChanged(object s, EventArgs e)
	{
		this.rpt.Groupings[0].Enabled = chk0.Value;
		this.rpt.Groupings[1].Enabled = chk1.Value;
	}
	protected void btnFlip_Click(object s, EventArgs e)
	{
		int? t = this.rpt.Groupings[0].Order;
		this.rpt.Groupings[0].Order = this.rpt.Groupings[1].Order;
		this.rpt.Groupings[1].Order = t;
	}
	protected void btnGenerate_Click(object s, EventArgs e)
	{
		this.Bind();
	}
}
class T : IPropertiesContainer
{
	public string A { get; set; }
	public string B { get; set; }
	public int C { get; set; }

	#region IPropertiesContainer Members

	public object GetValue(string name)
	{
		switch (name)
		{
			case "A": return this.A;
			case "B": return this.B;
			case "C": return this.C;
		}
		throw new NotImplementedException();
	}

	public void SetValue(string name, object value)
	{
		switch (name)
		{
			case "A": this.A = (string)value; break;
			case "B": this.B = (string)value; break;
			case "C": this.C = (int)value; break;
		}
		//throw new NotImplementedException();
	}

	#endregion
}
